/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package application;

/**
 *
 * @author suvam
 */
import java.awt.EventQueue;
import java.io.*;
import java.net.*;

public class KnockKnockClient {
    public static void main(String[] args) throws IOException {

        EventQueue.invokeLater(new Runnable() {
            public void run ()
            {

            }
        });

        Socket kkSocket = null;
        PrintWriter out = null;
        
        try {
            kkSocket = new Socket("localhost", 4444);
            out = new PrintWriter(kkSocket.getOutputStream(), true);
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: taranis.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: taranis.");
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        //String fromServer;
        String fromUser;

        InputDataThread th=new InputDataThread(kkSocket);
             th.start();
        while (true)
        {
            

            fromUser = stdIn.readLine();
             if(!th.isAlive())
                    break;

	    if (fromUser != null) {
                System.out.println("Me : " + fromUser);
                out.println(fromUser);
	    }

          
                if (fromUser.equals("Bye."))
                {
                    if(th.isAlive())
                        th.stop();
                      break;
                }
        }

        out.close();
        
        stdIn.close();
        kkSocket.close();
    }
}
